<template>
  <div class="dashboard-container">
    <el-row :gutter="20">
      <!-- 顶部统计卡片 -->
      <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-content">
            <div class="stat-icon" style="background-color: #409EFF;">
              <el-icon><User /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-title">用户总量</div>
              <div class="stat-value">{{ formatNumber(stats.userCount) }}</div>
              <div class="stat-compare">
                <span :class="stats.userGrowth >= 0 ? 'up' : 'down'">
                  {{ stats.userGrowth >= 0 ? '↑' : '↓' }} {{ Math.abs(stats.userGrowth) }}%
                </span>
                <span class="stat-label">同比</span>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-content">
            <div class="stat-icon" style="background-color: #67C23A;">
              <el-icon><Brush /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-title">设计师数量</div>
              <div class="stat-value">{{ formatNumber(stats.designerCount) }}</div>
              <div class="stat-compare">
                <span :class="stats.designerGrowth >= 0 ? 'up' : 'down'">
                  {{ stats.designerGrowth >= 0 ? '↑' : '↓' }} {{ Math.abs(stats.designerGrowth) }}%
                </span>
                <span class="stat-label">同比</span>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-content">
            <div class="stat-icon" style="background-color: #E6A23C;">
              <el-icon><VideoPlay /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-title">课程观看总量</div>
              <div class="stat-value">{{ formatNumber(stats.courseViews) }}</div>
              <div class="stat-compare">
                <span :class="stats.viewsGrowth >= 0 ? 'up' : 'down'">
                  {{ stats.viewsGrowth >= 0 ? '↑' : '↓' }} {{ Math.abs(stats.viewsGrowth) }}%
                </span>
                <span class="stat-label">同比</span>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 课程观看人数排行 -->
    <el-row :gutter="20" class="mt-20">
      <el-col :span="24">
        <el-card shadow="hover">
          <template #header>
            <span>热门课程观看排行</span>
          </template>
          <el-table :data="courseRankings" style="width: 100%" v-loading="loading">
            <el-table-column prop="rank" label="排名" width="80">
              <template #default="{ $index }">
                <el-tag :type="getRankTagType($index + 1)">
                  {{ $index + 1 }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="courseName" label="课程名称" />
            <el-table-column prop="instructor" label="讲师" width="150" />
            <el-table-column prop="views" label="观看人数" width="120" sortable>
              <template #default="{ row }">{{ formatNumber(row.views) }}</template>
            </el-table-column>
            <el-table-column prop="growth" label="增长率" width="120" sortable>
              <template #default="{ row }">
                <span :class="row.growth >= 0 ? 'up' : 'down'">
                  {{ row.growth >= 0 ? '↑' : '↓' }} {{ Math.abs(row.growth) }}%
                </span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="120">
              <template #default>
                <el-button type="text">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <!-- 用户增长趋势 -->
    <el-row :gutter="20" class="mt-20">
      <el-col :span="24">
        <el-card shadow="hover">
          <template #header>
            <span>用户增长趋势</span>
            <div>
              <el-radio-group v-model="timeRange" size="small" @change="loadData">
                <el-radio-button value="week">本周</el-radio-button>
                <el-radio-button value="month">本月</el-radio-button>
                <el-radio-button value="year">本年</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div style="height: 350px;">
            <v-chart class="chart" :option="userGrowthOption" autoresize />
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { LineChart } from 'echarts/charts'
import {
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent,
  ToolboxComponent,
  DataZoomComponent
} from 'echarts/components'
import VChart from 'vue-echarts'
import { User, Brush, VideoPlay } from '@element-plus/icons-vue'
import { getDashboardStats } from '/@/api/dashboard'

use([
  CanvasRenderer,
  LineChart,
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent,
  ToolboxComponent,
  DataZoomComponent
])

interface DashboardStats {
  userCount: number
  userGrowth: number
  designerCount: number
  designerGrowth: number
  courseViews: number
  viewsGrowth: number
  userGrowthTrend: Array<{
    date: string
    count: number
  }>
  courseRankings: Array<{
    courseName: string
    instructor: string
    views: number
    growth: number
  }>
}

const loading = ref(false)
const timeRange = ref('week')
const stats = ref<DashboardStats>({
  userCount: 0,
  userGrowth: 0,
  designerCount: 0,
  designerGrowth: 0,
  courseViews: 0,
  viewsGrowth: 0,
  userGrowthTrend: [],
  courseRankings: []
})

const courseRankings = ref<Array<{
  courseName: string
  instructor: string
  views: number
  growth: number
}>>([])

// 用户增长趋势图配置
const userGrowthOption = ref({
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    data: [] as string[],
    axisLabel: {
      rotate: 45
    }
  },
  yAxis: {
    type: 'value',
    name: '用户数'
  },
  series: [
    {
      name: '新增用户',
      type: 'line',
      smooth: true,
      data: [] as number[],
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [{
            offset: 0,
            color: 'rgba(64, 158, 255, 0.8)'
          }, {
            offset: 1,
            color: 'rgba(64, 158, 255, 0.1)'
          }]
        }
      },
      itemStyle: {
        color: '#409EFF'
      },
      emphasis: {
        focus: 'series'
      }
    }
  ]
})

const formatNumber = (num: number) => {
  return num.toLocaleString('zh-CN')
}

const getRankTagType = (rank: number) => {
  if (rank === 1) return 'danger'
  if (rank === 2) return 'warning'
  if (rank === 3) return 'success'
  return ''
}

const loadData = async () => {
  loading.value = true
  try {
    const res = await getDashboardStats({ timeRange: timeRange.value })
    stats.value = res.data
    
    // 更新用户增长趋势图数据
    userGrowthOption.value.xAxis.data = res.data.userGrowthTrend.map(item => item.date)
    userGrowthOption.value.series[0].data = res.data.userGrowthTrend.map(item => item.count)
    
    // 更新课程排行数据
    courseRankings.value = res.data.courseRankings
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.dashboard-container {
  padding: 20px;
}

.stat-card {
  margin-bottom: 20px;
  cursor: pointer;
}

.stat-content {
  display: flex;
  align-items: center;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
}

.stat-icon .el-icon {
  font-size: 30px;
  color: white;
}

.stat-info {
  flex: 1;
}

.stat-title {
  font-size: 14px;
  color: #909399;
  margin-bottom: 5px;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 5px;
}

.stat-compare {
  font-size: 12px;
}

.stat-compare .up {
  color: #f56c6c;
}

.stat-compare .down {
  color: #67c23a;
}

.stat-label {
  color: #909399;
  margin-left: 5px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart {
  width: 100%;
  height: 100%;
}

.mt-20 {
  margin-top: 20px;
}

.up {
  color: #67c23a;
}

.down {
  color: #f56c6c;
}
</style>